package chatManagers;

/*
 * addchat.java
 *
 * Created on August 29, 2006, 1:27 PM
 */

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author spring
 * @version
 */
public class addChat extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
        
        //DBMSManager dbm= new DBMSManager(request.getParameter("db"));
        DBMSManager dbm = new DBMSManager();
        String cname = request.getParameter("cname");
        String cdesc = request.getParameter("cdesc");
        String chatType = request.getParameter("chatType");
        int owner = dbm.checkLogin(request.getRemoteUser());
        int chatRestrict = new Integer(request.getParameter("cauth")).intValue();
        
        String userString = "";
        String dString = "";
        
        // Who may participate in the chat?
        switch (chatRestrict) {
            case 0:    // Everyone
                userString = "*";
                dString = "Everyone";
                break;
            case 1:    // Restrict by group
                String[] groups = request.getParameterValues("cgroup");
                for(int i=0; i<groups.length; i++) {
                    userString += groups[i] + ", ";
                    dString += groups[i] + "<br>";
                }
                break;
            case 2:    // Restrict by user
                userString = "*";
                break;
            default:
                userString = "*";
                break;
        }
        
        try {
            int status = dbm.addNewChat(cname, cdesc, chatRestrict, chatType, userString, owner);
            out.println("<p><b><span style=\"color:009900;\">The chat room &quot;" +
                    cname + "&quot; has been added successfully!</span></b></p>");
            if(chatType.equals("expert")) {
                out.println("<p>Please note that the chat room will close automatically" +
                        " in 24 hours (in case you forget to End the Chat yourself.)");
            }
            out.println("<p>Finally, <a href=\"ListUsers\">click here to ensure participants" +
                    " can access the chat room!</a><br /><br />Users must be part of the chat group to participate!</p>");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbm.closeConnection();}
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
